package com.tianma315.wx.view;

import com.tianma315.core.common.exception.MessageException;

import com.tianma315.core.sys.domain.UserDO;
import com.tianma315.wx.service.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

/**
 * 首页
 * <p>
 * Created by zcm on 2018/5/14.
 */
@Controller
public class WxMainController {

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @Autowired
    private UserServices userService;

    @GetMapping(value = {"/","/wx/main"})
    ModelAndView login() {

        return new ModelAndView("home");
    }

    @GetMapping(value = "/wx/socialRegister")
    public ModelAndView register(Map<String, String> map) {
        return new ModelAndView("register", map);
    }

    @PostMapping("/wx/bindUser")
    public String register(UserDO user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = user.getUsername();
        UserDO result = userService.getUserDOByUserName(userName);
        if (result == null) {
            throw new MessageException("没有此用户");
        }
        //将业务系统的用户与社交用户绑定
        providerSignInUtils.doPostSignUp(userName, new ServletWebRequest(request));
        return "login";
    }

}
